//
//  TYZCustomSelectedView.m
//  ESTSCRMAPP
//
//  Created by 李贤立 on 2018/11/1.
//  Copyright © 2018年 Capfer. All rights reserved.
//

#import "TYZCustomSelectedView.h"

@interface TYZCustomSelectedView ()
/**
 *  所有的标签
 */
@property(nonatomic, strong) NSArray<TYZButton *> *buttonArray;
@end

@implementation TYZCustomSelectedView
- (instancetype)init {
    if (self = [super init]) {
        self.selectedNumber = -1;
        self.backgroundColor = ESTBackgroundColor;
    }
    return self;
}
- (void)setTitleArray:(NSArray<NSString *> *)titleArray {
    _titleArray = titleArray;
    [self setupUI];
}
- (UIColor *)selectedColor {
    if (_selectedColor == nil) {
        _selectedColor = ThemeColor;
    }
    return _selectedColor;
}
- (void)didClickCoverButton:(TYZButton *)button {
    NSInteger count = button.tag - TAG_LIBRARY_SelectedView_Buttons;
    if (self.didSelectedBlock) {
        self.didSelectedBlock(count);
    }
}
- (void)setSelectedNumber:(NSInteger)selectedNumber {
    _selectedNumber = selectedNumber;
    [self.buttonArray enumerateObjectsUsingBlock:^(TYZButton * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == selectedNumber) {
            [obj setBackgroundColor:self.selectedColor];
            [obj setTitleColor:TYZWhiteColor forState:UIControlStateNormal];
        } else {
            [obj setBackgroundColor:self.backgroundColor];
            [obj setTitleColor:TYZBlackColor forState:UIControlStateNormal];
        }
    }];
}
- (void)setupUI {
    if (self.titleArray.count > 0) {
        dispatch_async(dispatch_get_main_queue(), ^{
            CGFloat width = CGRectGetWidth(self.bounds) / self.titleArray.count;
            CGFloat height = CGRectGetHeight(self.bounds);
            self.layer.cornerRadius = height * 0.5;
            NSMutableArray *arrM = [NSMutableArray arrayWithCapacity:self.titleArray.count];
            [self.titleArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                TYZButton *button = [[TYZButton alloc] init];
                [self addSubview:button];
                [button mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.bottom.equalTo(self);
                    make.width.mas_equalTo(width);
                    make.left.equalTo(self).offset(width * idx);
                }];
                [button setTitle:obj forState:UIControlStateNormal];
                button.titleLabel.font = TYZFont(CoreInfo.fontSize * 0.8);
                button.titleLabel.textAlignment = NSTextAlignmentCenter;
                button.layer.cornerRadius = height * 0.5;
                button.layer.masksToBounds = YES;
                button.tag = TAG_LIBRARY_SelectedView_Buttons + idx;
                [button addTarget:self action:@selector(didClickCoverButton:) forControlEvents:UIControlEventTouchUpInside];
                [arrM addObject:button];
                if (self.selectedNumber == idx) {
                    [button setBackgroundColor:self.selectedColor];
                    [button setTitleColor:TYZWhiteColor forState:UIControlStateNormal];
                } else {
                    [button setTitleColor:TYZBlackColor forState:UIControlStateNormal];
                    [button setBackgroundColor:self.backgroundColor];
                }
            }];
            self.buttonArray = [arrM mutableCopy];
        });
    }
}
- (void)dealloc {
    
}
@end
